﻿using System;
using Newtonsoft.Json.Linq;
using TrinityAPI.Base;
using TrinityAPI.Base.Tools;
using DouYin.Open.Platform.Tools;

namespace DouYin.Open.Platform
{
    public class GetSign : IAction
    {
        public JToken Invoke(JObject dataset, params JToken[] args)
        {
            var query = dataset.GetJsonObject("query");
            string appkey = query.GetJsonObjectStringValue("app_key");
            string method = query.GetJsonObjectStringValue("method");
            string timestamp = query.GetJsonObjectStringValue("timestamp");
            string version = query.GetJsonObjectStringValue("v", "2");

            string secret = null;
            var constant = dataset.GetJsonObject("constant");
            var signCheckConfig = constant.GetJsonObject("signCheckConfig");
            var accounts = signCheckConfig.GetJsonObject("accounts");
            if (!string.IsNullOrEmpty(appkey) && accounts != null)
            {
                secret = accounts.GetJsonObject(appkey).GetJsonObjectStringValue("secret");
            }

            var requestString = dataset.GetJsonObjectStringValue("requestString");

            string sign = ApiUtils.Sign(appkey, secret, method, timestamp, version, requestString);

            return sign;
        }
    }
}
